﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Xml;

namespace PizzaLunch
{
    public abstract class TweetSource
    {
        public string ShowName { get; set;}
        private string _id;
        public string ID { get{return _id;} }
        public string Logo { get; set; }

        public abstract bool Query(TweetAccount tac);
        public abstract bool QueryUserTimeline(TweetAccount tac, UserInfo user);
        public abstract void GetUserInfo(TweetAccount tac);
        public abstract void GetUserInfoByID(TweetAccount tac, UserInfo user);
        public abstract void GetComments(TweetAccount tac, TweetItem ti);
        public abstract bool Compose(TweetAccount tac, string content);
        public abstract bool Retweet(TweetAccount tac, TweetItem ti, string Content);
        public abstract bool Comment(TweetAccount tac, TweetItem ti, string Content);
        public abstract void GetFansList(TweetAccount tac, UserInfo userInfo, UserInfo.UserType uType, int index);
        public abstract bool DeleteTweet(TweetAccount tac, TweetItem ti);
        public abstract string GetItemUrl(TweetItem ti);

        protected Util.SourceConfig _sourceCfg;
        public Util.SourceConfig SourceCfg
        {
            get { return _sourceCfg; }
        }

        public abstract void CreateSetting();

        protected TweetSource(string id)
        {
            _id = id;
            _sourceCfg = new Util.SourceConfig();
            if (!LoadSetting())
            {
                CreateSetting();
                SaveSetting();
            }
        }

        public bool LoadSetting()
        {
            return SourceCfg.Load(ID);
        }

        public void SaveSetting()
        {
            SourceCfg.Save(ID);
        }

        public bool Login(out string accessToken, out string accessSecrect)
        {
            string VerifyUrl, tempKey, tempSecrect;
            accessToken = null;
            accessSecrect = null;

            List<Parameter> parameters = new List<Parameter>();
            oAuth.OauthKey requestKey = new oAuth.OauthKey();
            string queryString = null;

            try
            {
                requestKey.customKey = SourceCfg.customKey;
                requestKey.customSecrect = SourceCfg.customSecrect;
                requestKey.callbackUrl = SourceCfg.callbackUrl;
                oAuth.GetOauthUrl(SourceCfg.baseUrl + oAuth.Command_Request, HttpRequest.HTTP_GET, requestKey, parameters, out queryString);

                HttpRequest request = new HttpRequest();
                string retString = request.HttpGet(SourceCfg.baseUrl + oAuth.Command_Request, queryString);
                oAuth.ParseToken(retString, out tempKey, out tempSecrect);

                oAuth.OauthKey accessKey = new oAuth.OauthKey();
                accessKey.customKey = SourceCfg.customKey;
                accessKey.customSecrect = SourceCfg.customSecrect;
                accessKey.tokenKey = tempKey;
                accessKey.tokenSecrect = tempSecrect;

                VerifyUrl = SourceCfg.baseUrl + oAuth.Command_Verify + "?" + oAuth.OAuthTokenKey + "=" + accessKey.tokenKey;

                OauthVerify verify = new OauthVerify(VerifyUrl);
                if (verify.ShowDialog() != true)
                    return false;

                accessKey.verify = verify.VerifyCode;
                parameters.Clear();

                oAuth.GetOauthUrl(SourceCfg.baseUrl + oAuth.Command_Access, HttpRequest.HTTP_GET, accessKey, parameters, out queryString);
                retString = request.HttpGet(SourceCfg.baseUrl + oAuth.Command_Access, queryString);
                oAuth.ParseToken(retString, out tempKey, out tempSecrect);
                accessToken = tempKey;
                accessSecrect = tempSecrect;
                return true;
            }
            catch (Exception e)
            {
                MainControl.Logger.WriteLog("Login error with " + ID + " "
                    + e.ToString(), LoggerFile.LoggerLevel.LoggerError);
                return false;
            }
        }
    }
}
